package com.ljyh.forum.controller;

import com.ljyh.forum.entity.User;
import com.ljyh.forum.service.CategoryService;
import com.ljyh.forum.service.PostService;
import com.ljyh.forum.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/admin")
@Tag(name = "管理员接口", description = "管理员操作相关接口")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private PostService postService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/users")
    @Operation(summary = "获取所有用户列表")
    public ApiResponse<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ApiResponse.success("获取用户列表成功", users);
    }

    @PutMapping("/users/{id}/ban")
    @Operation(summary = "封禁用户")
    public ApiResponse<Void> banUser(@PathVariable Long id) {
        boolean success = userService.banUser(id);
        if (success) {
            return ApiResponse.success("封禁成功", null);
        } else {
            return ApiResponse.error(400, "封禁失败，用户可能不存在");
        }
    }

    @PutMapping("/users/{id}/unban")
    @Operation(summary = "解封用户")
    public ApiResponse<Void> unbanUser(@PathVariable Long id) {
        boolean success = userService.unbanUser(id);
        if (success) {
            return ApiResponse.success("解封成功", null);
        } else {
            return ApiResponse.error(400, "解封失败，用户可能不存在");
        }
    }

    @DeleteMapping("/posts/{id}")
    @Operation(summary = "删除帖子")
    public ApiResponse<Void> deletePost(@PathVariable Long id) {
        boolean success = postService.deletePost(id);
        if (success) {
            return ApiResponse.success("删除成功", null);
        } else {
            return ApiResponse.error(400, "删除失败，帖子可能不存在");
        }
    }

    @PostMapping("/categories")
    @Operation(summary = "添加分类")
    public ApiResponse<Void> addCategory(@RequestBody CategoryRequest request) {
        boolean success = categoryService.addCategory(request.getName());
        if (success) {
            return ApiResponse.success("添加成功", null);
        } else {
            return ApiResponse.error(400, "添加失败，分类名称可能已存在");
        }
    }

    @PutMapping("/categories/{id}")
    @Operation(summary = "修改分类")
    public ApiResponse<Void> updateCategory(@PathVariable Integer id, @RequestBody CategoryRequest request) {
        boolean success = categoryService.updateCategory(id, request.getName());
        if (success) {
            return ApiResponse.success("修改成功", null);
        } else {
            return ApiResponse.error(400, "修改失败，分类名称可能已存在");
        }
    }

    @DeleteMapping("/categories/{id}")
    @Operation(summary = "删除分类")
    public ApiResponse<Void> deleteCategory(@PathVariable Integer id) {
        boolean success = categoryService.deleteCategory(id);
        if (success) {
            return ApiResponse.success("删除成功", null);
        } else {
            return ApiResponse.error(400, "删除失败，分类可能不存在或包含帖子");
        }
    }

    @PutMapping("/posts/{id}/featured")
    @Operation(summary = "设置帖子精华状态")
    public ApiResponse<Void> setPostFeatured(@PathVariable Long id, @RequestParam Integer isFeatured) {
        if (isFeatured != 0 && isFeatured != 1) {
            return ApiResponse.error(400, "参数错误，isFeatured只能为0或1");
        }
        
        boolean success = postService.setPostFeatured(id, isFeatured);
        if (success) {
            String message = isFeatured == 1 ? "设置精华成功" : "取消精华成功";
            return ApiResponse.success(message, null);
        } else {
            return ApiResponse.error(400, "操作失败，帖子可能不存在");
        }
    }

    /**
     * 分类请求体
     */
    public static class CategoryRequest {
        private String name;

        // getters and setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
    }

    /**
     * 统一API响应格式
     */
    public static class ApiResponse<T> {
        private int code;
        private String msg;
        private T data;

        public ApiResponse(int code, String msg, T data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }

        public static <T> ApiResponse<T> success(String msg, T data) {
            return new ApiResponse<>(200, msg, data);
        }

        public static <T> ApiResponse<T> error(int code, String msg) {
            return new ApiResponse<>(code, msg, null);
        }

        // getters
        public int getCode() { return code; }
        public String getMsg() { return msg; }
        public T getData() { return data; }
    }
}
